package jenetics.test;

import org.jenetics.BitChromosome;
import org.jenetics.BitGene;
import org.jenetics.Genotype;
import org.jenetics.Mutator;
import org.jenetics.Phenotype;
import org.jenetics.RouletteWheelSelector;
import org.jenetics.SinglePointCrossover;
import org.jenetics.engine.Engine;
import org.jenetics.engine.EvolutionStatistics;

import static org.jenetics.engine.limit.bySteadyFitness;
import static org.jenetics.engine.EvolutionResult.toBestPhenotype;

public class OnesCounting {

	public static void main(String[] args) {
		final Engine<BitGene, Integer> engine = 
				Engine.builder(OnesCounting::count, BitChromosome.of(20, 0.15)).populationSize(500).selector(new RouletteWheelSelector<>()).alterers(new Mutator<>(0.55), new SinglePointCrossover<>(0.06))
				.build();
		
		final EvolutionStatistics<Integer, ?> statistics = EvolutionStatistics.ofNumber();
		
		final Phenotype<BitGene, Integer> best = engine.stream().limit(bySteadyFitness(7)).limit(1000).peek(statistics).collect(toBestPhenotype());
		
		System.out.println(statistics);
		System.out.println(best);
	}

	private static Integer count(final Genotype<BitGene> gt) {
		return ((BitChromosome)gt.getChromosome()).bitCount();
	}
}